/**
 * 
 */
package org.vsg.stock.basic.service.volfigure;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.vsg.stock.basic.domain.VolFigureDetails;
import org.vsg.stock.basic.domain.VolFigureKlineDaily;
import org.vsg.stock.basic.domain.VolFigureKlineDailyPK;
import org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor;
import org.vsg.stock.core.domain.StockDailyKLine;
import org.vsg.stock.core.indicator.kline.ZigIndicatorResultItem;

/**
 *  处理缩量柱模式3，连续介升量缩，过顶触底做多
 *  
 * @author Bill Vison
 *
 */
public class DecreaseMod3Processor implements VolFigureKlineDailyProcessor {

	static private DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	
	
	private Set<String> dailyKeys = new HashSet<String>();	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#processDailyData(java.util.List)
	 */
	@Override
	public List<VolFigureKlineDaily> processDailyData(
			List<StockDailyKLine> stkDailyKLineList) {

		List<VolFigureKlineDaily> klineDailyList = new Vector<VolFigureKlineDaily>();		
		
		double rate = 0.95;
		
		StockDailyKLine startLeveKline = null;
			
		for (int i = 0 ; i < stkDailyKLineList.size()-3 ; i++) {
			startLeveKline = stkDailyKLineList.get(i);
			double levelClose = startLeveKline.getClose();
			long levelVol = startLeveKline.getVol();
			short markTimes = 0;
			// --- today's price is update 
			if (startLeveKline.getYieldRate() < 0 ) {
				continue;
			}
			
			double preRecordMaxPrice = 0;
			for (int j = 1 ; j <4 ; j++) {
				
				StockDailyKLine indexLevelKline = stkDailyKLineList.get(i+j);
				
				// --- break; the level kline ---
				if (indexLevelKline.getVol() >  levelVol ) {
					markTimes = 0;
					break;
				}
				
				// --- the first vol must less than  0.95 * levelVol
				if (j == 1) {
					if ( indexLevelKline.getVol() > levelVol * rate) {
						// --- return to out the loop
						markTimes = 0;
						break;
					}
				}
				
				if ( j == 3) {
					if ( preRecordMaxPrice > indexLevelKline.getClose() ) {
						markTimes = 0;
						break;
					}
				} else {
					preRecordMaxPrice = preRecordMaxPrice < indexLevelKline.getClose() ? indexLevelKline.getClose() : preRecordMaxPrice;
				}
				
				
				// --- mark the close level ---
				if ( indexLevelKline.getClose() >= levelClose ) {
					markTimes++;
				}
			}
			
			if ( markTimes > 1) {
				// --- after 3 days

				String indexLine = startLeveKline.getId().getStockCode()+","+detail.getCode()+","+df.format( startLeveKline.getDate() );
				
				if (!dailyKeys.contains( indexLine) ) {
					// --- add to date for handle ---

					VolFigureKlineDailyPK idPk = new VolFigureKlineDailyPK();
					idPk.setCodeVolFigureDetails( this.detail.getCode() );
					idPk.setStockCode( startLeveKline.getId().getStockCode() );
					idPk.setStockStartDate( startLeveKline.getId().getDate() );

					// --- gen new data object ---
					VolFigureKlineDaily dailyInst = new VolFigureKlineDaily();
					dailyInst.setId( idPk );
					dailyInst.setStockDateEnd( stkDailyKLineList.get(i+3).getDate() );
					klineDailyList.add( dailyInst );
					
				}				
				i+=3;
			}

			
		}
		

		return klineDailyList;
	}
	
	private List<ZigIndicatorResultItem> crests;

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCrestsInCycle(java.util.List)
	 */
	@Override
	public void setCrestsInCycle(List<ZigIndicatorResultItem> crests) {
		this.crests = crests;

	}

	@Override
	public void setExistedVolFigureKineDaily(
			List<VolFigureKlineDaily> volFigureKlineDailyList) {
		// --- get the daily ---
		VolFigureKlineDailyPK id = null;
		for (VolFigureKlineDaily daily : volFigureKlineDailyList) {
			id = daily.getId();
			String keyLine = id.getStockCode()+ "," + id.getCodeVolFigureDetails()+"," + df.format( id.getStockStartDate() );
			dailyKeys.add( keyLine );
		}
		
	}

	private VolFigureDetails detail;	
	
	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setCurrentVolFigureDetails(org.vsg.stock.basic.domain.VolFigureDetails)
	 */
	@Override
	public void setCurrentVolFigureDetails(VolFigureDetails detail) {
		this.detail = detail;

	}

	/* (non-Javadoc)
	 * @see org.vsg.stock.basic.service.handler.VolFigureKlineDailyProcessor#setTroughsInCycle(java.util.List)
	 */
	@Override
	public void setTroughsInCycle(List<ZigIndicatorResultItem> troughs) {
		// TODO Auto-generated method stub

	}

}
